package com.vchello.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.vchello.bean.agency.Agency;
import com.vchello.bean.agency.AgencyApplyRemark;
import com.vchello.bean.bind.ProjectQuery;
import com.vchello.bean.pro.Pro;
import com.vchello.bean.security.UserRole;
import com.vchello.bean.user.User;
import com.vchello.controller.BaseController;
import com.vchello.controller.constants.SubjectAttrConstants;
import com.vchello.service.agency.AgencyApplyRemarkService;
import com.vchello.service.agency.AgencyService;
import com.vchello.service.project.ProjectService;
import com.vchello.service.security.UserRoleService;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/admin/agency")
public class AgencyAdminController extends BaseController {
	
	@Autowired
	private AgencyService agencyService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private AgencyApplyRemarkService agencyApplyRemarkService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model, Page<Agency> page, Agency agency){
		page = agencyService.selectPage(page, new EntityWrapper<Agency>(agency));
		model.addAttribute("pages", page);
		return "admin/agencies/agenciesList";
		
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String listAjax(Model model, Page<Agency> page, Agency agency){
		page = agencyService.selectPage(page, new EntityWrapper<Agency>(agency));
		model.addAttribute("pages", page);
		return "admin/agencies/query/agencyQuery";
		
	}
	
	@RequestMapping(value = "/{fatherId}/agencyMember", method = RequestMethod.GET)
	public String agencyMember(Model model, Page<UserRole> page, @PathVariable int fatherId){
		User user = new User();
		user.setId(fatherId);
		page = userRoleService.findUserRoleByFatherId(page, user, 0);
		model.addAttribute("pages", page).addAttribute("fatherId", fatherId);
		return "admin/agencies/agenciesNum";
		
	}
	
	@RequestMapping(value = "/{fatherId}/agencyMember", method = RequestMethod.POST)
	public String agencyMemberAjax(Model model, Page<UserRole> page, @PathVariable int fatherId, User user){
		user.setId(fatherId);
		page = userRoleService.findUserRoleByFatherId(page, user, 0);
		model.addAttribute("pages", page).addAttribute("fatherId", fatherId);;
		return "admin/agencies/query/agenciesNumQuery";
		
	}
	
	@RequestMapping(value = "/{userId}/detal", method = RequestMethod.GET)
	public String proDetail(Model model, Page<Pro> page, @PathVariable int userId){
		ProjectQuery query = new ProjectQuery();
		query.setUserId(userId);
		query.setUserType(1);
		page = projectService.queryPage(page, query);
		model.addAttribute("pages", page).addAttribute("userId", userId);
		return "admin/agencies/agenciesPro";
		
	}
	
	@RequestMapping(value = "/{userId}/detal", method = RequestMethod.POST)
	public String proDetail(Model model, Page<Pro> page, @PathVariable int userId, ProjectQuery query){
		query.setUserId(userId);
		query.setUserType(1);
		page = projectService.queryPage(page, query);
		model.addAttribute("pages", page).addAttribute("userId", userId);
		return "admin/agencies//query/agenciesProQuery";
		
	}
	
	@RequestMapping(value = "/applyList", method = RequestMethod.GET)
	public String applyList(Page<Agency> page, Model model, Agency agency){
		page = agencyService.findAgencyApply(page, agency);
		model.addAttribute("pages", page);
		return "admin/apply/applyList";
		
	}
	
	@RequestMapping(value = "/applyListAjax", method = RequestMethod.POST)
	public String applyListAjax(Page<Agency> page, Model model, Agency agency){
		page = agencyService.findAgencyApply(page, agency);
		model.addAttribute("pages", page);
		return "admin/apply/query/agencyQuery";
		
	}
	
	@RequestMapping(value = "/saveAgencyRemark", method = RequestMethod.POST)
	public void saveAgencyRemark(int applyId, AgencyApplyRemark agencyApplyRemark){
		User user = (User) session.getAttribute(SubjectAttrConstants.USER_AWARE_CONSTANT);
		agencyApplyRemark.setUserId((null == user) ? 0 : user.getId());
		agencyApplyRemark.setApplyId(applyId);
		agencyApplyRemark.setCreateTime(Utility.getSimpleFormatedDayNow());
		boolean isTrue = agencyApplyRemarkService.insert(agencyApplyRemark);
		if(isTrue){
			success("添加备注成功");
			return;
		}
		
		error("添加备注失败");
	}
	
	@RequestMapping(value = "/remarkList/{applyId}", method = RequestMethod.GET)
	public String remarkList(Model model, Page<AgencyApplyRemark> page, AgencyApplyRemark agencyApplyRemark, @PathVariable int applyId){
		agencyApplyRemark.setApplyId(applyId);
		page = agencyApplyRemarkService.findAgencyApplyRemarkByPage(page, agencyApplyRemark);
		model.addAttribute("pages", page);
		return "admin/apply/remark";
		
	}
	
	@RequestMapping(value = "/remarkDetail/{id}", method = RequestMethod.GET)
	public String remarkDetail(Model model, @PathVariable int id){
		AgencyApplyRemark agencyApplyRemark = agencyApplyRemarkService.findAgencyApplyRemarkById(id);
		model.addAttribute("agencyApplyRemark", agencyApplyRemark);
		return "admin/apply/remarkDetail";
		
	}
	
	@RequestMapping(value = "/updateAgencyRemark", method = RequestMethod.POST)
	public void updateAgencyRemark(int id, String remark){
		AgencyApplyRemark agencyApplyRemark = agencyApplyRemarkService.findAgencyApplyRemarkById(id);
		agencyApplyRemark.setRemark(remark);
		boolean isTrue = agencyApplyRemarkService.insertOrUpdate(agencyApplyRemark);
		if(isTrue){
			success("修改备注成功");
			return;
		}
		
		error("修改备注失败");
	}
	
	@RequestMapping(value = "/deleteAgencyApplyRemark", method = RequestMethod.POST)
	public void deleteAgencyApplyRemark(int id){
		AgencyApplyRemark agencyApplyRemark = agencyApplyRemarkService.findAgencyApplyRemarkById(id);
		if(null == agencyApplyRemark){
			error("备注为空");
			return;
		}
		boolean isTure = agencyApplyRemarkService.deleteAgencyApplyRemark(agencyApplyRemark);
		if(isTure) {
			success("删除成功");
			return;
		}
		error("删除失败");
	}
}
